import os
from google.appengine.ext.webapp import template
from django import newforms as forms
from django.newforms.util import ErrorList

class Renderable:
	def render(self, tpl, data):
		path = os.path.join(os.path.dirname(__file__), 'tpl', tpl)
		self.response.out.write(template.render(path, data))

class TableForm(forms.Form):
#	def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
#				 initial=None, error_class=ErrorList, label_suffix=':'):
#		self.is_bound = data is not None or files is not None
#		self.data = data or {}
#		self.files = files or {}
#		self.auto_id = auto_id
#		self.prefix = prefix
#		self.initial = initial or {}
#		self.error_class = DivErrorList
#		self.label_suffix = label_suffix
#		self._errors = None 
#		self.fields = deepcopy(self.base_fields)

	def as_table(self):
		"Display errors after the field"
		return self._html_output(u'<tr><th>%(label)s</th><td>%(field)s%(errors)s%(help_text)s</td></tr>', 
				u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)

class DivErrorList(ErrorList):
	def __unicode__(self):
		return self.as_divs()
	def as_divs(self):
		if not self: return u''
		return u'<span class="error">%s</span>' % ''.join([u'%s ' % e for e in self])

